package com.epoint.androidphone.mobileoa.ui.mail;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;

import com.epoint.androidmobile.bizlogic.common.FileDownService;
import com.epoint.androidmobile.bizlogic.mail.model.MailAttachModel;
import com.epoint.androidmobile.bizlogic.mail.task.Mail_ConvertTask;
import com.epoint.androidmobile.bizlogic.mail.task.Mail_GetDetail_Task;
import com.epoint.androidmobile.bizlogic.mail.task.Mail_GetList_Task;
import com.epoint.androidmobile.bizlogic.mail.task.Mail_TaskEndTask;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time 2013-5-6 下午5:26:37
 * @annomation
 */
public class MailInBoxDetailView extends SuperPhonePage {

	/**
	 * 普通邮件
	 */
	public static final String NOT_TASK_MAIL = "0";
	/**
	 * 任务邮件
	 */
	public static final String IS_TASK_MAIL = "1";
	/**
	 * 已经结束的任务邮件
	 */
	public static final String END_TASK_MAIL = "2";

	WebView wv;
	List<MailAttachModel> attachList = new ArrayList<MailAttachModel>();
	String MailGuid, boxType;
	long getMailDetailTaskId, transferTaskMailTaskId, endTaskMailTaskId;
	Button btHf, btZf, btFj;
	String Subject, ToUserDispNameList, ToUserGuidList, SENDTIME, MailContent,
			FROMDISPNAME, FromUserGuid;
	/**
	 * 邮件状态 0:普通邮件 1:任务邮件 2:已经结束的任务邮件
	 */
	String NeedTaskMonitor;
	LinearLayout llOperation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.mailinboxdetailview, "邮件详情");
		boxType = getIntent().getStringExtra("boxType");
		MailGuid = getIntent().getStringExtra("mailguid");
		getData(MailGuid);
		wv = (WebView) findViewById(R.id.wv);
		btHf = (Button) findViewById(R.id.btHf);
		btZf = (Button) findViewById(R.id.btZf);
		btFj = (Button) findViewById(R.id.btFj);
		btFj.setOnClickListener(this);
		btZf.setOnClickListener(this);
		btHf.setOnClickListener(this);
		llOperation = (LinearLayout) findViewById(R.id.llOperate);
		if (Mail_GetList_Task.OUTBOX_TYPE.equals(boxType)) {
			btZf.setVisibility(View.GONE);
			btHf.setVisibility(View.GONE);
		}
		if (!Mail_GetList_Task.TASK_INBOX_TYPE.equals(boxType)
				|| !Mail_GetList_Task.TASK_OUTBOX_TYPE.equals(boxType)) {
			getBtRight().setVisibility(View.VISIBLE);
			getBtRight().setText("操作");
		}
	}

	private void getData(String _mailguid) {
		showTopProgressBar();
		HashMap<String, Object> param = getPassMap();
		param.put("MailGuid", _mailguid);

		getMailDetailTaskId = new Mail_GetDetail_Task(this, param).startTask();
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if (getMailDetailTaskId == taskId) {
			if (validateXML(obj)) {
				String bs = obj.toString();
				Subject = StringHelp.getXMLAtt(bs, "Subject");
				SENDTIME = StringHelp.getXMLAtt(bs, "SENDTIME");
				MailContent = StringHelp.getXMLAtt(bs, "MailContent");
				MailContent = StringHelp.XMLSwitchedOrigion(MailContent);
				FROMDISPNAME = StringHelp.getXMLAtt(bs, "FROMDISPNAME");
				NeedTaskMonitor = StringHelp.getXMLAtt(bs, "NeedTaskMonitor");
				ToUserDispNameList = StringHelp.getXMLAtt(bs,
						"ToUserDispNameList");
				ToUserGuidList = StringHelp.getXMLAtt(bs, "ToUserGuidList");
				String SecretUserDispNameList = StringHelp.getXMLAtt(bs,
						"SecretUserDispNameList");
				String SecretUserGuidList = StringHelp.getXMLAtt(bs,
						"SecretUserGuidList");
				String ChaoSongUserGuidList = StringHelp.getXMLAtt(bs,
						"ChaoSongUserGuidList");
				String ChaoSongUserDispNameList = StringHelp.getXMLAtt(bs,
						"ChaoSongUserDispNameList");
				String HasAttach = StringHelp.getXMLAtt(bs, "HasAttach");
				FromUserGuid = StringHelp.getXMLAtt(bs, "FromUserGuid");
				String chaosongren = "";
				if (ChaoSongUserDispNameList.trim().length() > 0) {
					chaosongren = String.format(
							"<div id='epoint_sender'>抄送人：%s</div><hr/>",
							ChaoSongUserDispNameList);
				}
				String disContent = String
						.format("%s<div id='oschina_title'>%s</div><div id='oschina_outline'>%s %s</div></div><hr/><div id='epoint_sender'>收件人：%s</div><hr />%s%s",
								StringHelp.OschinaStyle, Subject, FROMDISPNAME,
								SENDTIME, ToUserDispNameList, chaosongren,
								MailContent);
				System.out.println(disContent);
				wv.loadDataWithBaseURL(null, disContent, "text/html", "utf-8",
						null);

				if ("1".equals(HasAttach)) {
					String ATTACHListXML = StringHelp.getXMLAttOut(bs,
							"ATTACHList");
					attachList = XMLUtil.DomAnalysisCommon(ATTACHListXML,
							MailAttachModel.class);
					btFj.setText("附件(" + attachList.size() + ")");
				} else {
					btFj.setVisibility(View.GONE);
					if (boxType.equals(Mail_GetList_Task.OUTBOX_TYPE)) {
						llOperation.setVisibility(View.GONE);
					}
				}

			}
		} else if (taskId == transferTaskMailTaskId) {
			if (validateXML(obj)) {
				NeedTaskMonitor = IS_TASK_MAIL;
				ToastUtil.toastShort(this, "当前邮件已经转成任务邮件");
			}
		} else if (taskId == endTaskMailTaskId) {
			if (validateXML(obj)) {
				NeedTaskMonitor = END_TASK_MAIL;
				ToastUtil.toastShort(this, "当前任务邮件已经完成结束操作");
			}
		}
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == btFj) {
			if (attachList.size() == 0) {
				return;
			}
			String[] attstr = new String[attachList.size()];
			for (int i = 0; i < attachList.size(); i++) {
				attstr[i] = attachList.get(i).ATTACHFILENAME;
			}
			AlertUtil.showAlertMenu(this, "附件下载", attstr,
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							MailAttachModel m = attachList.get(which);
							FileDownService.download(getContext(),
									getString(R.string.WebServiceType),
									m.DownLoadUrl, m.isBigFile, MailGuid,
									m.ATTACHFILENAME,
									InitView.attachdownMainPath);
						}
					});
		} else if (v == btHf) {
			Intent intent = new Intent(this, MailSendView.class);
			intent.putExtra("EnterFlag", 2);
			intent.putExtra("MailTitle", Subject);
			intent.putExtra("ToUserDispNameList", FROMDISPNAME + ";");
			intent.putExtra("ToUserGuidList", FromUserGuid + ";");
			intent.putExtra("MailContent", MailContent);
			intent.putExtra("FROMDISPNAME", FROMDISPNAME);
			intent.putExtra("SENDTIME", SENDTIME);
			startActivity(intent);
		} else if (v == btZf) {
			Intent intent = new Intent(this, MailSendView.class);
			intent.putExtra("EnterFlag", 3);
			intent.putExtra("MailTitle", Subject);
			intent.putExtra("ZFAttachSize", attachList.size());
			String[] filenames = new String[attachList.size()];
			String[] fileguids = new String[attachList.size()];

			for (int i = 0; i < attachList.size(); i++) {
				filenames[i] = attachList.get(i).ATTACHFILENAME;
				fileguids[i] = attachList.get(i).AttachGuid;
			}
			intent.putExtra("filenames", filenames);
			intent.putExtra("fileguids", fileguids);

			intent.putExtra("MailContent", MailContent);
			intent.putExtra("FROMDISPNAME", FROMDISPNAME);
			intent.putExtra("SENDTIME", SENDTIME);
			startActivity(intent);
		} else if (v == getBtRight()) {
			// 任务邮件的处理
			if (IS_TASK_MAIL.equals(NeedTaskMonitor)) {
				AlertUtil.showAlertMenu(this, "操作选项", new String[] { "结束任务",
						"反馈信息" }, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if (which == 0) {
							showDialogMiddle("结束任务邮件中...");
							HashMap<String, Object> param = getPassMap();
							param.put("MailGuid", MailGuid);
							endTaskMailTaskId = new Mail_TaskEndTask(
									MailInBoxDetailView.this, param)
									.startTask();
						} else if (which == 1) {
							// 跳转到任务邮件反馈列表界面
							Intent intent = new Intent(
									MailInBoxDetailView.this,
									MailFeedBackListView.class);
							intent.putExtra("MailGuid", MailGuid);
							startActivity(intent);
						}
					}
				});
				// 非任务邮件的处理
			} else if (NOT_TASK_MAIL.equals(NeedTaskMonitor)) {
				AlertUtil.showAlertMenu(this, "操作选项", new String[] { "转任务邮件" },
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								showDialogMiddle("转任务邮件中...");
								HashMap<String, Object> param = getPassMap();
								param.put("MailGuid", MailGuid);
								transferTaskMailTaskId = new Mail_ConvertTask(
										MailInBoxDetailView.this, param)
										.startTask();
							}
						});
				// 已经结事的任务邮件
			} else if (END_TASK_MAIL.equals(NeedTaskMonitor)) {
				AlertUtil.showAlertMenu(this, "操作选项", new String[] { "反馈信息" },
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// 跳转到任务邮件反馈列表界面
								Intent intent = new Intent(
										MailInBoxDetailView.this,
										MailFeedBackListView.class);
								intent.putExtra("MailGuid", MailGuid);
								intent.putExtra("NeedTaskMonitor",
										NeedTaskMonitor);
								startActivity(intent);
							}
						});
			}
		}
	}

}
